<template>
  <ContentWrap>
    <!--    顶部查询项-->
    <el-form :model="form" inline>
      <el-form-item :label="t('sn.sn_number')">
        <el-input
          v-model="form.sn_id"
          clearable
          :placeholder="t('common.inputText')"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item :label="t('wholesalOrder.productName')">
        <el-input
          v-model="form.product_name"
          clearable
          :placeholder="t('common.inputText')"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item :label="t('sn.change_time')">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="-"
          start-placeholder="起始时间"
          end-placeholder="结束时间"
          @change="changeTime"
          :editable="false"
          unlink-panels
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="initialize">{{
          t('common.search')
        }}</el-button>
        <el-button icon="Refresh" @click="reset">{{ t('common.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!--    操作项-->
    <div class="flex flex-items-center mb-20px mt-10px">
      <el-button type="primary" @click="handleAdd">
        {{ t('sn.scan_code') }}
      </el-button>
      <!-- <el-button type="primary" @click="handleAdd">
        {{ t('action.exportFile') }}
      </el-button>
      <el-button type="primary" @click="handleAdd">
        {{ t('sn.print') }}
      </el-button> -->
    </div>
    <!-- 表格主体 -->
    <el-table
      :data="dataList"
      highlight-current-row
      stripe
      border
      v-loading="tableLoading"
      :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{ textAlign: 'center' }"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column :label="t('invoice.id')" prop="id" min-width="80" />
      <el-table-column :label="t('sn.sn_number')" prop="sn_id" min-width="200" />
      <el-table-column
        :label="t('wholesalOrder.productName')"
        prop="product_name"
        min-width="300"
      />
      <!-- <el-table-column :label="t('purchase.sup')" prop="seller_name" min-width="200" /> -->
      <el-table-column
        :label="t('sn.operator')"
        prop="creator_name"
        min-width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('sn.change_operation')"
        min-width="100"
        prop="action"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('sn.change_time')"
        prop="create_time"
        min-width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('sn.related_warehouse')"
        min-width="100"
        prop="warehouse_name"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <!--    分页-->
    <Pagination
      :total="paginationTotal"
      v-model:page="form.pageIndex"
      v-model:limit="form.pageSize"
      @pagination="initialize"
    />
  </ContentWrap>
  <!--    新增/编辑弹框-->
  <Add ref="dialogStaffRef" @refresh="initialize" />
</template>

<script setup lang="ts">
import Add from './components/add.vue'
import dayjs from 'dayjs'
import Pagination from '@/components/Pagination/index.vue'
import { ContentWrap } from '@/components/ContentWrap'
import { getSnlogListApi, getSnEnumApi } from '@/api/snManage'

const { t } = useI18n()

const tableLoading = ref<boolean>(false)
const dataList = ref<any[]>([])

const dialogStaffRef = ref()

const paginationTotal = ref<number>(0)
const form = ref<Record<string, any>>({
  sn_id: '',
  product_name: '',
  pageIndex: 1,
  pageSize: 20
})
const date = ref<any>([])
const statusList = ref<Array<Record<string, any>>>([])
const actionList = ref<Array<Record<string, any>>>([])
//获取枚举
const getDropDownBoxInfo = async () => {
  const resp = await getSnEnumApi({ field: 'all' })
  statusList.value = resp.data.status
  actionList.value = resp.data.action
}
//初始化请求
const initialize = async () => {
  try {
    tableLoading.value = true
    // 这里需要替换为实际的员工信息获取API
    // delete form.value.date
    const resp = await getSnlogListApi(form.value)
    dataList.value = resp.data.list
    paginationTotal.value = resp.data.total
    tableLoading.value = false
  } catch (e) {
    tableLoading.value = false
  }
}

//重置查询项
const reset = () => {
  form.value = {
    sn_id: '',
    product_name: '',
    pageIndex: 1,
    pageSize: 20
  }
  date.value = []
  initialize()
}

onMounted(() => {
  getDropDownBoxInfo()
  initialize()
})

//根据接口枚举匹配出中文名称
const useEnumeratePicker = (
  enumList: any[],
  field: string | number,
  key?: string,
  value?: string
) => {
  if (!enumList) return
  const matchItem =
    enumList.find((item: Record<string, any>) => item[value ?? 'value'] === field) || {}
  return matchItem[key ?? 'label'] || ''
}
//点击新增
const handleAdd = () => {
  nextTick(() => {
    dialogStaffRef.value.dialogVisible = true
    dialogStaffRef.value.started = false
    dialogStaffRef.value.actionList = actionList.value
  })
}
const changeTime = (e: Date[]) => {
  if (e && e.length > 0) {
    form.value.start_time = dayjs(e[0]).format('YYYY-MM-DD 00:00:00')
    form.value.end_time = dayjs(e[1]).format('YYYY-MM-DD 23:59:59')
  } else {
    form.value.start_time = null
    form.value.end_time = null
  }
}
</script>

<style lang="less" scoped></style>
